/*
 * Copyright (c) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef XPM_COMMON_H
#define XPM_COMMON_H

#include <string>
#include <unistd.h>

#include "code_sign_attr_utils.h"
#include "xpm_log.h"

namespace OHOS {
namespace Security {
namespace XpmUnitTest {
struct XpmRegionArea {
    uint64_t start;
    uint64_t end;
};

const std::string XPM_LIB_PATH = "/system/lib/libxpm.z.so";
const std::string XPM_PROC_PREFIX_PATH = "/proc/";
const std::string XPM_PROC_SUFFIX_PATH = "/xpm_region";
const std::string XPM_DEBUG_FS_MODE_PATH = "/sys/kernel/debug/xpm/xpm_mode";
const std::string SELINUX_MODE_PATH = "/sys/fs/selinux/enforce";

const std::string SELINUX_TYPE_NWEBSPAWN = "u:r:isolated_render:s0";
const std::string SELINUX_TYPE_DEBUG_HAP = "u:r:debug_hap:s0";
const std::string SELINUX_TYPE_SH = "u:r:sh:s0";
const std::string PERMISSIVE_MODE = "0";
const std::string ENFORCE_MODE = "1";

constexpr unsigned long XPM_PROC_LENGTH = 50;
constexpr unsigned long MAP_XPM = 0x40;

constexpr unsigned long SUM_FUN_OFFSET = 0x0;
constexpr unsigned long ANON_SIZE = 0x1000;
constexpr unsigned long NEW_ANON_SIZE = ANON_SIZE;

const unsigned long PAGE_SIZE = (sysconf(_SC_PAGESIZE));
const unsigned long PAGE_MASK = ~(PAGE_SIZE - 1);

constexpr unsigned int OP_READ = 0;
constexpr unsigned int OP_WRITE = 1;
constexpr unsigned int OP_WRITE_ONLY = 2;

typedef int (*SumFunc)(int, int);

int GetXpmRegion(struct XpmRegionArea &area);
bool IsXpmRegionInner(unsigned long start, unsigned long end);
bool IsXpmRegionOuter(unsigned long start, unsigned long end);

int ExecSumFun(void *addr);
int AccessData(void *addr, int op);

int SetCon(const std::string &type);

int GetDataSegment(const std::string &path, unsigned long &start, unsigned long &end);
int GetCodeSegment(const std::string &path, unsigned long &start, unsigned long &end);

} // namespace XpmUnitTest
} // namespace Security
} // namespace OHOS

#endif